package com.lebang.controller.sys.cences;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.domain.CencesBusMember;
import com.lebang.feign.AppInfoFeign;
import com.lebang.feign.CencesHundredPointFeign;
import com.lebang.model.RequestUserInfo;
import com.lebang.service.cences.CencesHundredPointService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * 百倍时间金币统计
 *
 * @author ljx
 * @date 2020-11-27
 */
@Controller
@RequestMapping("/sys/cenceshundredpoint")
public class CencesHundredPointController {
    @Autowired
    private CencesHundredPointFeign feign;
    @Autowired
    private AppInfoFeign appInfoFeign;
    @Autowired
    private CencesHundredPointService service;

    @GetMapping("/list")
    public String list(Model model) {
        JsonData list = appInfoFeign.list();
        model.addAttribute("appInfoList", list.getData());
        return "/cenceshundredpoint/list";
    }


    @Login
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit, String appStore, Integer star, Integer end, Integer appId) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        if (!TextUtil.isEmpty(appId)) {
            userInfo.setAppId(appId);
        }
        JsonData data = feign.selectData(userInfo, page, limit, appStore, star, end);

        return JSONObject.parseObject(JSONObject.toJSONString(data.getData()));
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id,Integer time) {
        return feign.deleteById(time);
    }

    @Login
    @PostMapping("/lastCheck")
    @ResponseBody
    public String cen(HttpServletRequest request, String date) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate end = LocalDate.parse(date, fmt);
        LocalDateTime dateTime = LocalDateTime.of(end.plusDays(1), LocalTime.MIN);
        List<CencesBusMember> activeMemberList = service.activeMemberList(dateTime);
        List<String> list = service.appStoreList();
        JsonData data = feign.starDataInit(userInfo, dateTime, JSONObject.toJSONString(activeMemberList), JSONObject.toJSONString(list));
        if(data.isSuccess()){
            return "success";
        }
        return "fail";
    }
}
